package com.jiaai.util.qq;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;

/**
 * class_name:QQUtils
 * describe:------------------>获取qq信息
 * create_user:张昉平
 * create_date:2018/2/28
 * create_time:18:46
 */
public class QQUtils {

    private static final String GET_USER_INFO = "http://openapi.tencentyun.com/v3/user/get_info";

    public static JSONObject getUserInfo(){
        CloseableHttpClient client = HttpClientBuilder.create().build();
        JSONObject object = null;
        try {
            HashMap<String,String> params = new HashMap<>();
            params.put("openid", "B624064BA065E01CB73F835017FE96FA");
            params.put("openkey", "5F154D7D2751AEDC8527269006F290F70297B7E54667536C");
            params.put("appid", "2");
            params.put("pf", "qzone");
            params.put("format", "json");
            params.put("userip", "112.90.139.30");

            String sig = SnsSigCheck.encodeUrl(SnsSigCheck.makeSig("get", "/v3/user/get_info", params, "screte"));
            params.put("sig", sig);

            params.put("openid", SnsSigCheck.encodeUrl("B624064BA065E01CB73F835017FE96FA"));
            params.put("openkey", SnsSigCheck.encodeUrl("5F154D7D2751AEDC8527269006F290F70297B7E54667536C"));
            params.put("appid", SnsSigCheck.encodeUrl("2"));
            params.put("pf", SnsSigCheck.encodeUrl("qzone"));
            params.put("format", SnsSigCheck.encodeUrl("json"));
            params.put("userip", SnsSigCheck.encodeUrl("112.90.139.30"));

            StringBuilder sb = new StringBuilder();
            for(String s : params.keySet()){
                sb.append("&").append(s).append("=").append(params.get(s));
            }

            HttpGet httpGet = new HttpGet(GET_USER_INFO.concat("?").concat(sb.substring(1)));
            CloseableHttpResponse response = client.execute(httpGet);
            String res = EntityUtils.toString(response.getEntity(), "UTF-8");
            object = JSONObject.parseObject(res);
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return object;
    }

    public static void main(String[] args) {
        System.out.println(getUserInfo());
    }
}
